<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="手机号" prop="userMobile">
        <el-input
          v-model="queryParams.userMobile"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="busid">
        <el-input
          v-model="queryParams.busid"
          placeholder="请输入订单编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="orderList">
      <el-table-column label="订单编号" align="center" prop="busid" />
      <el-table-column label="手机号" align="center" prop="userMobile" />
      <el-table-column label="冻结金额" align="center" prop="money">
        <template slot-scope="scope">
          <span v-if="scope.row.addRed">+</span>
          <span v-else>-</span>
          <span>{{ scope.row.money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="rema" />
      <el-table-column label="操作时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="cateName">
          <el-input v-model="form.cateName" placeholder="请输入分类名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  productCateList,
  insertCate,
  detailCate,
  modifyCate,
  delCate,
} from "@/api/product/productCate";
import { userFreezeData } from "@/api/customer/customer";
import { getSassToken } from "@/utils/utils";
import { Debounce } from "@/utils/tools.js";
import imageDrapUpload from "@/components/Upload";
export default {
  name: "productCate",
  components: {
    imageDrapUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 日期范围
      dateRange: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      typeList: [],
      // 弹出层标题
      title: "订单发货",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        userMobile: "",
        busid: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cateName: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if (this.dateRange && this.dateRange.length > 0) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      } else {
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
      userFreezeData(this.queryParams).then((response) => {
        this.orderList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品分类";
    },
    /** 删除按钮操作 */
    handleDelete: Debounce(function (row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.cateName + '"的数据项？')
        .then(function () {
          return delCate(row.cateId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    }, 500),
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate: Debounce(function (row) {
      this.reset();
      detailCate(row.cateId).then((response) => {
        if (response.code === 200) {
          if (response.data && response.data.cateIcon) {
            response.data.cateIcon = [{ url: response.data.cateIcon }];
          }
          this.form = response.data;
          this.open = true;
          this.title = "修改商品分类";
        }
      });
    }, 500),
    /** 提交按钮 */
    submitForm: Debounce(function (row) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = {
            cateName: this.form.cateName,
          };
          if (this.form.cateId) {
            (data.cateId = this.form.cateId),
              modifyCate(data).then((response) => {
                this.$modal.msgSuccess("编辑成功");
                this.open = false;
                this.getList();
              });
          } else {
            insertCate(data).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }, 500),
  },
};
</script>
<style lang="scss" scoped></style>
